package com.example.mydy.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mydy.R;
import com.example.mydy.model.Advertisement;
import com.squareup.picasso.Picasso;

import java.util.List;

public class AdvertisementAdapter extends RecyclerView.Adapter<AdvertisementAdapter.ViewHolder> {
    private Context context;
    private List<Advertisement> advertisements;
    private OnItemClickListener listener;
    
    public interface OnItemClickListener {
        void onItemClick(int position);
        void onEditClick(int position);
        void onDeleteClick(int position);
    }
    
    public AdvertisementAdapter(Context context, List<Advertisement> advertisements) {
        this.context = context;
        this.advertisements = advertisements;
    }
    
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
    
    public void updateData(List<Advertisement> newAdvertisements) {
        this.advertisements = newAdvertisements;
        notifyDataSetChanged();
    }
    
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_advertisement, parent, false);
        return new ViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Advertisement ad = advertisements.get(position);
        holder.tvTitle.setText(ad.getTitle());
        holder.tvDescription.setText(ad.getContent());
        
        // 设置简短的发布日期
        String date = ad.getDate();
        if (date != null && !date.isEmpty()) {
            holder.tvAdDate.setText(date);
        } else {
            holder.tvAdDate.setVisibility(View.GONE);
        }
        
        holder.tvAdStatus.setText(ad.getStatus());
        
        // 使用Picasso加载图片
        String imageUrl = ad.getImageUrl();
        Log.d("AdvertisementAdapter", "Loading image for ad " + ad.getId() + ": " + imageUrl);
        
        if (imageUrl != null && !imageUrl.isEmpty()) {
            holder.ivAdImage.setVisibility(View.VISIBLE);
            Picasso.get()
                .load(imageUrl)
                .placeholder(R.drawable.placeholder_image)
                .error(R.drawable.placeholder_image)
                .into(holder.ivAdImage, new com.squareup.picasso.Callback() {
                    @Override
                    public void onSuccess() {
                        Log.d("AdvertisementAdapter", "Image loaded successfully for ad " + ad.getId());
                    }

                    @Override
                    public void onError(Exception e) {
                        Log.e("AdvertisementAdapter", "Error loading image for ad " + ad.getId() + ": " + e.getMessage());
                    }
                });
        } else {
            Log.d("AdvertisementAdapter", "No image URL for ad " + ad.getId());
            holder.ivAdImage.setVisibility(View.GONE);
        }
        
        // 确保编辑和删除按钮可见
        holder.btnEdit.setVisibility(View.VISIBLE);
        holder.btnDelete.setVisibility(View.VISIBLE);
        
        // 设置编辑和删除按钮的点击事件
        holder.btnEdit.setOnClickListener(v -> {
            if (listener != null) {
                listener.onEditClick(holder.getAdapterPosition());
            }
        });
        
        holder.btnDelete.setOnClickListener(v -> {
            if (listener != null) {
                listener.onDeleteClick(holder.getAdapterPosition());
            }
        });
    }
    
    @Override
    public int getItemCount() {
        return advertisements.size();
    }
    
    public class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView ivAdImage;
        public TextView tvTitle;
        public TextView tvDescription;
        public TextView tvAdDate;
        public TextView tvAdStatus;
        public Button btnEdit;
        public Button btnDelete;

        public ViewHolder(View itemView) {
            super(itemView);
            ivAdImage = itemView.findViewById(R.id.iv_ad_image);
            tvTitle = itemView.findViewById(R.id.tv_ad_title);
            tvDescription = itemView.findViewById(R.id.tv_ad_description);
            tvAdDate = itemView.findViewById(R.id.tv_ad_date);
            tvAdStatus = itemView.findViewById(R.id.tv_ad_status);
            btnEdit = itemView.findViewById(R.id.btn_edit);
            btnDelete = itemView.findViewById(R.id.btn_delete);
            
            itemView.setOnClickListener(v -> {
                if (listener != null && getAdapterPosition() != RecyclerView.NO_POSITION) {
                    listener.onItemClick(getAdapterPosition());
                }
            });
        }
    }
} 